package com.changgou.util;

import java.security.MessageDigest;
import java.util.UUID;

public class MD5Util {


    /**
     * 生成随机字符18位
     *
     * @param
     */
    static String generateSalt(){

        String uuidMd5 = null;
        try {
            uuidMd5 = UUID.randomUUID().toString().replaceAll("-","");
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            byte[] bytes = md5.digest(uuidMd5.getBytes("UTF-8"));

            StringBuffer sb=  new StringBuffer();

            for (int i = 0; i < 10; i++) {

                sb.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100).substring(1, 3));

            }

            uuidMd5=sb.toString();


         //   uuidMd5=new String(md5.digest(uuidMd5.getBytes("UTF-8"))) ;

        } catch (Exception e) {
            e.printStackTrace();
        }

        //截取的uuid生成的前10位
        return uuidMd5.substring(0,10);

    }


    /**
     *
     *      MD5 方法运算
     *
     */




    public static String[] getMD5(String password,String salt){
        String[] strings=new String[2];

        if (password ==null || password.length()<0){
            return null;
        }

        if (salt == null || salt.length()<0 ||salt.equals("")){

            return null;
        }

        try {

            MessageDigest md5 = MessageDigest.getInstance("MD5");


            byte[] bytes = md5.digest(password.getBytes("UTF-8"));
            StringBuffer sb=  new StringBuffer();
            for (int i = 0; i < 10; i++) {

                //202cb962ac59075b964b
                //202cb962ac59075b964b07152d234b70

                sb.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100).substring(1, 3));

            }

            //下标位0的 代表了盐
            strings[0]=salt;


            //加盐拼接
            strings[1]=strings[0]+sb.toString();

        } catch (Exception e) {

            return null;
        }
        return strings;

    }

    //方法重载  getMD5
    public static String[] getMD5(String password){
        return getMD5(password,generateSalt());
    }


    public static Boolean flig(String password,String mod5) throws Exception {

        if (password == ""|| password ==null){
            return false;
        }
        if (mod5 == ""|| mod5 ==null){
            return false;
        }


        try {
            // 转码过程
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            byte[] bytes = md5.digest(password.getBytes("UTF-8"));

            StringBuffer sb=  new StringBuffer();

            for (int i = 0; i < 10; i++) {

                sb.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100).substring(1, 3));

            }

            String s = new String(sb);

            //截取前5位数值
            String substring = mod5.substring(10);

            //比较
            boolean equals = s.equals(substring);

            return equals;
        } catch (Exception e) {
            return false;
        }
    }


    public static void main(String[] args) throws Exception {

        //生成的mds的密文
        String[] md5 = getMD5("123456");
        //下表为0的是盐
        System.out.println(md5[0]);
        //下标位1的是密文
        System.out.println(md5[1]);


        Boolean flig = flig("123456", md5[1]);
        System.out.println(flig);

    }
}
